package com.huike.web.controller.system;

import com.huike.clues.domain.dto.GetLogListDTO;
import com.huike.clues.domain.dto.SysOperLogDTO;
import com.huike.clues.domain.result.PageLogVO;
import com.huike.clues.service.ISysLogService;
import com.huike.common.constant.MagicConstants;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description SysLogController
 * @Author zhuzi
 * @Date 2023/10/17
 */
@RequestMapping("/monitor/operlog")
@RequiredArgsConstructor
@RestController
@Slf4j
public class SysLogController {

    private final ISysLogService service;

    /**
     * 获取日志列表
     */
    @GetMapping("/list")
    public PageLogVO getLogList(GetLogListDTO dto) {
        if (dto.getParams() != null && dto.getParams().get(MagicConstants.BEGIN_TIME) != null)
            dto.setBeginTime((String) dto.getParams().get(MagicConstants.BEGIN_TIME));
        if (dto.getParams() != null && dto.getParams().get(MagicConstants.END_TIME) != null)
            dto.setEndTime((String) dto.getParams().get(MagicConstants.END_TIME));
        log.info("Get dto: {}", dto);
        return service.getLogList(dto);
    }

    /**
     * 批量删除
     *
     * @param operIds 待删除的日志id集合
     */
    @DeleteMapping("/{operIds}")
    public AjaxResult<?> delete(@PathVariable List<Long> operIds) {
        log.info("Get operIds: {}", operIds);
        service.delete(operIds);
        return AjaxResult.success();
    }

    /**
     * 清空日志
     */
    @DeleteMapping("/clean")
    public AjaxResult<?> clean() {
        service.clean();
        return AjaxResult.success();
    }

    /**
     * 导出日志
     */
    @GetMapping("/export")
    public AjaxResult<?> export(GetLogListDTO dto) {
        log.info("Get SyslogPageVO: {}", dto);
        List<SysOperLogDTO> list = service.export(dto);
        ExcelUtil<SysOperLogDTO> util = new ExcelUtil<>(SysOperLogDTO.class);
        return util.exportExcel(list, "操作日志");
    }

}
